home *** CD-ROM | disk | FTP | other *** search
/ SGI Developer Toolbox 6.1 / SGI Developer Toolbox 6.1 - Disc 1.iso / toolbox / src / tutorials / custEducation / opengl2 / examples / accum / blur.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-11-11  |  4.5 KB  |  195 lines

  1. /*
  2.  * Copyright 1993, 1995, Silicon Graphics, Inc.
  3.  * All Rights Reserved.
  4.  *
  5.  * This is UNPUBLISHED PROPRIETARY SOURCE CODE of Silicon Graphics, Inc.;
  6.  * the contents of this file may not be disclosed to third parties, copied or
  7.  * duplicated in any form, in whole or in part, without the prior written
  8.  * permission of Silicon Graphics, Inc.
  9.  *
  10.  * RESTRICTED RIGHTS LEGEND:
  11.  * Use, duplication or disclosure by the Government is subject to restrictions
  12.  * as set forth in subdivision (c)(1)(ii) of the Rights in Technical Data
  13.  * and Computer Software clause at DFARS 252.227-7013, and/or in similar or
  14.  * successor clauses in the FAR, DOD or NASA FAR Supplement. Unpublished -
  15.  * rights reserved under the Copyright Laws of the United States.
  16.  */
  17.  
  18. /*  blur.c 
  19.  *  This program demonstrates using the accumulation buffer for
  20.  *  effects like motion blur.
  21.  * 
  22.  *    Escape key               - exit the program
  23.  */
  24. #include <GL/gl.h>
  25. #include <GL/glu.h>
  26. #include <GL/glut.h>
  27.  
  28. #include <math.h>
  29. #include <stdio.h>
  30.  
  31. /*  Function Prototypes  */
  32.  
  33. GLvoid  initgfx( GLvoid );
  34. GLvoid  drawScene( GLvoid );
  35. GLvoid  reshape( GLsizei, GLsizei );
  36. GLvoid  animate( GLvoid );
  37. GLvoid  visibility( GLint );
  38. GLvoid  keyboard( GLubyte, GLint, GLint );
  39.  
  40. GLvoid  printHelp( char * );
  41.  
  42. /* Global Definitions */
  43.  
  44. #define KEY_ESC    27    /* ascii value for the escape key */
  45.  
  46. /* Global Variables */
  47.  
  48. static GLfloat        incAngle, azimAngle, distance;
  49. static GLint        xStart, yStart;
  50.  
  51. static GLfloat        spin = 0.0;
  52.  
  53. GLvoid
  54. main( int argc, char *argv[] )
  55. {
  56.     GLsizei     width, height;
  57.  
  58.     glutInit( &argc, argv );
  59.  
  60.     width = glutGet( GLUT_SCREEN_WIDTH ); 
  61.     height = glutGet( GLUT_SCREEN_HEIGHT );
  62.     glutInitWindowPosition( width/4, height/4 ); 
  63.     glutInitWindowSize( width/4, height/4 );
  64.     glutInitDisplayMode( GLUT_RGBA|GLUT_DEPTH|GLUT_DOUBLE|GLUT_ACCUM );
  65.     glutCreateWindow( argv[0] );
  66.     
  67.     initgfx();
  68.  
  69.     glutKeyboardFunc( keyboard );
  70.     glutReshapeFunc( reshape );
  71.     glutIdleFunc( animate ); 
  72.     glutVisibilityFunc( visibility ); 
  73.     glutDisplayFunc( drawScene ); 
  74.  
  75.     printHelp( argv[0] );
  76.  
  77.     glutMainLoop();
  78. }
  79.  
  80. GLvoid
  81. printHelp( char *progname )
  82. {
  83.     fprintf(stdout, 
  84.         "\n%s - motion blurring using the accumulation buffer\n\n"
  85.         "Expect it to run very slowly on systems without \n"
  86.         "a hardware accumulation buffer.\n\n"
  87.         "Escape key        - exit the program\n\n",
  88.         progname);
  89. }
  90.  
  91. GLvoid
  92. initgfx( void )
  93. {
  94.     static GLfloat mat_ambient[] = { 0.25, 0.05, 0.4, 1.0 };
  95.     static GLfloat mat_diffuse[] = { 0.5, 0.1, 0.8, 1.0 };
  96.     static GLfloat mat_specular[] = { 1.0, 1.0, 1.0, 1.0 };
  97.         static GLfloat mat_shininess[] = { 10.0 };
  98.  
  99.     static GLfloat light_position[] = { 1.0, 1.0, 1.0, 0.0 };
  100.     static GLfloat lmodel_ambient[] = { 0.2, 0.2, 0.2, 1.0 };
  101.  
  102.     glMaterialfv(GL_FRONT, GL_AMBIENT, mat_ambient);
  103.     glMaterialfv(GL_FRONT, GL_DIFFUSE, mat_diffuse);
  104.     glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular);
  105.     glMaterialfv(GL_FRONT, GL_SHININESS, mat_shininess);
  106.  
  107.     glLightfv(GL_LIGHT0, GL_POSITION, light_position);
  108.     glLightModelfv(GL_LIGHT_MODEL_AMBIENT, lmodel_ambient);
  109.  
  110.     glEnable(GL_LIGHTING);
  111.     glEnable(GL_LIGHT0);
  112.  
  113.     glClearColor( 0.0, 0.0, 0.0, 1.0 );
  114.  
  115.     glClearAccum( 0.0, 0.0, 0.0, 0.0 );
  116.     glClear( GL_ACCUM_BUFFER_BIT );
  117.  
  118.     glEnable( GL_DEPTH_TEST );
  119.     glEnable( GL_CULL_FACE );
  120.  
  121.     distance = 4.5;
  122.     incAngle = 25.0;
  123.     azimAngle = 0.0;
  124. }
  125.  
  126. GLvoid 
  127. keyboard( GLubyte key, GLint x, GLint y )
  128. {
  129.     switch (key) {
  130.     case KEY_ESC:    /* Exit whenever the Escape key is pressed */
  131.         exit(0);
  132.     }
  133. }
  134.  
  135. GLvoid
  136. reshape( GLsizei width, GLsizei height )
  137. {
  138.     GLdouble     aspect;
  139.  
  140.     glViewport( 0, 0, width, height );
  141.  
  142.     aspect = (GLdouble) width / (GLdouble) height;
  143.  
  144.     glMatrixMode( GL_PROJECTION );
  145.     glLoadIdentity();
  146.     gluPerspective( 45.0, aspect, 3.0, 7.0 );
  147.     glMatrixMode( GL_MODELVIEW );
  148.  
  149.     glClear( GL_ACCUM_BUFFER_BIT );
  150. }
  151.  
  152. GLvoid
  153. animate( GLvoid )
  154. {
  155.     spin = fmodf( spin + 10.0, 360.0 );    /* update spin */
  156.     glutPostRedisplay();            /* Tell GLUT to redraw */
  157. }
  158.         
  159. GLvoid
  160. visibility( int state )
  161. {
  162.     if (state == GLUT_VISIBLE) {
  163.         glutIdleFunc( animate );
  164.     } else {
  165.         glutIdleFunc( NULL );
  166.     }
  167. }
  168.  
  169. GLvoid
  170. drawScene( GLvoid )
  171. {
  172.     static GLfloat decayFactor = 0.5;
  173.  
  174.     glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
  175.  
  176.     glPushMatrix();
  177.  
  178.         glTranslatef( 0.0, 0.0, -distance);
  179.         glRotatef( incAngle, 1.0, 0.0, 0.0 );
  180.         glRotatef( azimAngle, 0.0, 0.0, 1.0 );
  181.  
  182.         glPushMatrix();
  183.             glAccum( GL_MULT, decayFactor );
  184.             glRotatef( spin, 0.0, 1.0, 0.0 );
  185.             glTranslatef( 1.0, 0.0, 0.0 );
  186.             glutSolidSphere( 0.5, 15, 15 );
  187.             glAccum( GL_ACCUM, (1.0-decayFactor) );
  188.             glAccum( GL_RETURN, 1.0 );
  189.         glPopMatrix();
  190.  
  191.     glPopMatrix();
  192.  
  193.     glutSwapBuffers();
  194. }
  195.